package com.jinxiangyang.commons.core.vo;

import com.jinxiangyang.commons.core.exception.ErrorCode;

import java.io.Serializable;
import java.util.List;


/**
 * ajax请求返回Json格式数据的封装 
 */
public class AjaxJson implements Serializable {

	private static final long serialVersionUID = 1L;    // 序列化版本号

	public int code;    // 状态码
	public String msg;    // 描述信息 
	public Object data; // 携带对象

	/**
	 * 返回code
	 *
	 * @return
	 */
	public int getCode() {
		return this.code;
	}

	/**
	 * 给msg赋值，连缀风格
	 */
	public AjaxJson setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public String getMsg() {
		return this.msg;
	}

	/**
	 * 给data赋值，连缀风格
	 */
	public AjaxJson setData(Object data) {
		this.data = data;
		return this;
	}

	/**
	 * 将data还原为指定类型并返回
	 */
	@SuppressWarnings("unchecked")
	public <T> T getData(Class<T> cs) {
		return (T) data;
	}

	// ============================  构建  ================================== 

	public AjaxJson(int code, String msg, Object data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	// 返回成功
	public static AjaxJson getSuccess() {
		return new AjaxJson(ErrorCode.CODE_SUCCESS, "操作成功", null);
	}

	public static AjaxJson getSuccess(String msg) {
		return new AjaxJson(ErrorCode.CODE_SUCCESS, msg, null);
	}

	public static AjaxJson getSuccess(String msg, Object data) {
		return new AjaxJson(ErrorCode.CODE_SUCCESS, msg, data);
	}

	public static AjaxJson getSuccess(Object data) {
		return new AjaxJson(ErrorCode.CODE_SUCCESS, "操作成功", data);
	}

	public static AjaxJson getSuccessArray(Object... data) {
		return new AjaxJson(ErrorCode.CODE_SUCCESS, "操作成功", data);
	}

	// 返回失败
	public static AjaxJson getError() {
		return new AjaxJson(ErrorCode.CODE_ERROR, "操作失败", null);
	}

	public static AjaxJson getError(Integer code,String msg) {
		return new AjaxJson(code, msg, null);
	}


	public static AjaxJson getError(String msg) {
		return new AjaxJson(ErrorCode.CODE_ERROR, msg, null);
	}

	// 返回警告 
	public static AjaxJson getWarning() {
		return new AjaxJson(ErrorCode.CODE_ERROR, "warning", null);
	}

	public static AjaxJson getWarning(String msg) {
		return new AjaxJson(ErrorCode.CODE_WARNING, msg, null);
	}

	// 返回未登录
	public static AjaxJson getNotLogin(String message) {
		return new AjaxJson(ErrorCode.CODE_NOT_LOGIN, "未登录，请登录后再次访问", null);
	}

	// 返回没有权限的 
	public static AjaxJson getNotJur(String msg) {
		return new AjaxJson(ErrorCode.CODE_NOT_JUR, msg, null);
	}

	// 返回一个自定义状态码的
	public static AjaxJson get(int code, String msg) {
		return new AjaxJson(code, msg, null);
	}



	// 返回，根据受影响行数的(大于0=操作成功，小于0=error)
	public static AjaxJson getByLine(int line) {
		if (line > 0) {
			return getSuccess("操作成功", line);
		}
		return getError("操作失败").setData(line);
	}

	// 返回，根据布尔值来确定最终结果的  (true=操作成功，false=error)
	public static AjaxJson getByBoolean(boolean b) {
		return b ? getSuccess("操作成功") : getError("操作失败");
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public String toString() {
		String data_string = null;
		if (data == null) {

		} else if (data instanceof List) {
			data_string = "List(length=" + ((List) data).size() + ")";
		} else {
			data_string = data.toString();
		}
		return "{"
				+ "\"code\": " + this.getCode()
				+ ", \"msg\": \"" + this.getMsg() + "\""
				+ ", \"data\": " + data_string
				+ "}";
	}


}
